import { takeEvery, put } from "@redux-saga/core/effects";
import axios from "axios";
import {
  getRecommendListFail,
  getRecommendListSuccess,
  GET_RECOMMEND_LIST,
  IGetRecommendListAction,
} from "../actions/recommend.action";

function* handleGetRecommendList(action: IGetRecommendListAction) {
  try {
    const { data } = yield axios.get(
      `http://localhost:3000/api/personalized?limit=${action.payload}`
    );
    yield put(getRecommendListSuccess(data));
  } catch (error) {
    yield put(getRecommendListFail());
  }
}

export default function* recommendSaga() {
  yield takeEvery(GET_RECOMMEND_LIST, handleGetRecommendList);
}
